using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace URLresearch
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Text = "URL查找";
            this.Size = new System.Drawing.Size(800, 600);
        }

        private void InitializeComponent()
        {
            // URL输入控件
            Label urlLabel = new Label
            {
                Text = "输入URL:",
                Location = new System.Drawing.Point(20, 20),
                Size = new System.Drawing.Size(60, 20)
            };

            TextBox urlTextBox = new TextBox
            {
                Location = new System.Drawing.Point(90, 20),
                Size = new System.Drawing.Size(500, 20),
                Text = "https://"
            };

            Button fetchButton = new Button
            {
                Text = "获取内容",
                Location = new System.Drawing.Point(600, 20),
                Size = new System.Drawing.Size(80, 23)
            };
            fetchButton.Click += (s, e) => FetchWebContent(urlTextBox.Text);
            // 结果显示控件
            TabControl resultTabControl = new TabControl
            {
                Location = new System.Drawing.Point(20, 60),
                Size = new System.Drawing.Size(740, 480)
            };
            TabPage phonePage = new TabPage("手机号码");
            ListBox phoneListBox = new ListBox
            {
                Dock = DockStyle.Fill,
                ScrollAlwaysVisible = true
            };
            phonePage.Controls.Add(phoneListBox);

            TabPage emailPage = new TabPage("电子邮件");
            ListBox emailListBox = new ListBox
            {
                Dock = DockStyle.Fill,
                ScrollAlwaysVisible = true
            };
            emailPage.Controls.Add(emailListBox);

            TabPage rawPage = new TabPage("原始内容");
            TextBox rawTextBox = new TextBox
            {
                Dock = DockStyle.Fill,
                Multiline = true,
                ScrollBars = ScrollBars.Both,
                ReadOnly = true
            };
            rawPage.Controls.Add(rawTextBox);

            resultTabControl.TabPages.Add(phonePage);
            resultTabControl.TabPages.Add(emailPage);
            resultTabControl.TabPages.Add(rawPage);
            // 状态标签
            Label statusLabel = new Label
            {
                Location = new System.Drawing.Point(20, 550),
                Size = new System.Drawing.Size(740, 20),
                Text = "准备就绪"
            };
            this.Controls.Add(urlLabel);
            this.Controls.Add(urlTextBox);
            this.Controls.Add(fetchButton);
            this.Controls.Add(resultTabControl);
            this.Controls.Add(statusLabel);

            this.Tag = new Dictionary<string, Control>
            {
                { "phoneListBox", phoneListBox },
                { "emailListBox", emailListBox },
                { "rawTextBox", rawTextBox },
                { "statusLabel", statusLabel }
            };
        }
        private async void FetchWebContent(string url)
        {
            var controls = (Dictionary<string, Control>)this.Tag;
            var phoneListBox = (ListBox)controls["phoneListBox"];
            var emailListBox = (ListBox)controls["emailListBox"];
            var rawTextBox = (TextBox)controls["rawTextBox"];
            var statusLabel = (Label)controls["statusLabel"];
            phoneListBox.Items.Clear();
            emailListBox.Items.Clear();
            rawTextBox.Clear();
                using (WebClient client = new WebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    string htmlContent = await client.DownloadStringTaskAsync(url);
                    rawTextBox.Text = htmlContent;
                    // 提取手机号码
                    ExtractPhoneNumbers(htmlContent, phoneListBox);
                    // 提取电子邮件
                    ExtractEmails(htmlContent, emailListBox);
                    statusLabel.Text = $"解析完成 - 找到 {phoneListBox.Items.Count} 个手机号码和 {emailListBox.Items.Count} 个电子邮件";
                }
            
        }

        private void ExtractPhoneNumbers(string text, ListBox listBox)
        {
            // 手机号正则表达式
            string pattern = @"(1[3-9]\d{9})|(\d{3,4}-\d{7,8})";
            Regex regex = new Regex(pattern);

            foreach (Match match in regex.Matches(text))
            {
                if (!listBox.Items.Contains(match.Value))
                {
                    listBox.Items.Add(match.Value);
                }
            }
        }

        private void ExtractEmails(string text, ListBox listBox)
        {
            // 电子邮件正则表达式
            string pattern = @"[\w\.-]+@[\w\.-]+\.\w+";
            Regex regex = new Regex(pattern);

            foreach (Match match in regex.Matches(text))
            {
                if (!listBox.Items.Contains(match.Value))
                {
                    listBox.Items.Add(match.Value);
                }
            }
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}